package com.universest.swordholder.viewmodel
import android.content.Intent
import androidx.lifecycle.ViewModel

open class BaseViewModel(protected val intent:Intent? = null) : ViewModel(){

    /**
     * 获取字符串类型的参数
     */
    fun getStringExtra(key: String, defaultValue: String = ""): String {
        return intent?.getStringExtra(key) ?: defaultValue
    }

    /**
     * 获取整数类型的参数
     */
    fun getIntExtra(key: String, defaultValue: Int = 0): Int {
        return intent?.getIntExtra(key, defaultValue) ?: defaultValue
    }

    /**
     * 获取布尔类型的参数
     */
    fun getBooleanExtra(key: String, defaultValue: Boolean = false): Boolean {
        return intent?.getBooleanExtra(key, defaultValue) ?: defaultValue
    }

    /**
     * 获取长整型参数
     */
    fun getLongExtra(key: String, defaultValue: Long = 0L): Long {
        return intent?.getLongExtra(key, defaultValue) ?: defaultValue
    }

    /**
     * 获取浮点型参数
     */
    fun getFloatExtra(key: String, defaultValue: Float = 0f): Float {
        return intent?.getFloatExtra(key, defaultValue) ?: defaultValue
    }

    /**
     * 获取双精度浮点型参数
     */
    fun getDoubleExtra(key: String, defaultValue: Double = 0.0): Double {
        return intent?.getDoubleExtra(key, defaultValue) ?: defaultValue
    }

    /**
     * 获取序列化对象参数
     */
    fun <T> getSerializableExtra(key: String, clazz: Class<T>): T? {
        @Suppress("UNCHECKED_CAST","DEPRECATION")
        return intent?.getSerializableExtra(key) as? T
    }

    /**
     * 获取Parcelable对象参数
     */
    fun <T> getParcelableExtra(key: String, clazz: Class<T>): T? {
        @Suppress("DEPRECATION")
        return intent?.getParcelableExtra(key)
    }

}